﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using PhysicsControlModule.Daos;
using PhysicsControlModule.Models;
using PhysicsControlModule.Security;

namespace PhysicsControlModule.Controllers
{
    public class StudentController : Controller
    {
        //
        // GET: /Student/

        [Authorize(Roles = "STUDENT")]
        public ActionResult Cabinet()
        {
            int currUserId = MmShProvider.GetUser().Id;
            List<CabinetModel> cabinetModels = new List<CabinetModel>();
            foreach (Labwork labwork in LabworkDao.LoadAvailableLabworks(currUserId))
            {
                User owner = UserDao.GetById(labwork.OwnerId);
                cabinetModels.Add(
                    new CabinetModel 
                        {
                            LabworkId = labwork.Id,
                            LabworkTitle = labwork.Title,
                            LabworkEnable =
                                AdmQuestionDao.GetAnsweredCount(labwork.Id, currUserId) < AdmQuestionDao.GetCount(labwork.Id) &&
                                CtrlQuestionDao.GetAnsweredCount(labwork.Id, currUserId) < CtrlQuestionDao.GetCount(labwork.Id),
                            Owner = string.Format("{0} {1}", owner.FirstName, owner.LastName),
                            AdmQuestAnsweredCount = AdmQuestionDao.GetAnsweredCount(labwork.Id, currUserId),
                            AdmQuestTotalCount = AdmQuestionDao.GetCount(labwork.Id),
                            CtrlQuestAnsweredCount = CtrlQuestionDao.GetAnsweredCount(labwork.Id, currUserId),
                            CtrlQuestTotalCount = CtrlQuestionDao.GetCount(labwork.Id)
                        });
            }
            return View(cabinetModels);
        }
    }
}
